David's Blog

Utilizing `uilive` for Real-Time Terminal Updates in Golang

By David Li on 2023-05-30T19:37:46.011Z

Utilizing uilive for Real-Time Terminal Updates in Golang

Golang, also known as Go, is a statically typed, compiled programming language created by Google. It has gained popularity for its simplicity, efficiency, and strong support for concurrent programming. In this article, we will explore how to use the uilive package to provide real-time updates in terminal applications developed in Go.

uilive is a Go package that allows developers to create dynamic, real-time updates in terminal applications with ease. This makes it perfect for displaying progress bars, loading spinners, and other animated text elements.

Installation

To get started, you need to install the uilive package. Run the following command:

go get -u github.com/gosuri/uilive

This command fetches the package and its dependencies and installs them in your Go workspace.

Basic Usage

Let’s walk through a simple example of how to use uilive to display real-time updates in a Go application. We’ll create a basic progress bar that updates as a simulated task completes.

First, import the uilive package:

import (
    "fmt"
    "time"

    "github.com/gosuri/uilive"
)

Next, create an instance of the uilive.Writer:

writer := uilive.New()

The uilive.Writer is an io.Writer that updates the terminal with the latest content. It overwrites previous content instead of appending to it, allowing for real-time updates.

Now, let’s start the uilive.Writer:

writer.Start()

This initializes the writer and prepares it to receive updates. After this point, you can write to the uilive.Writer instance, and it will handle updating the terminal in real-time.

For our example, let’s create a simple progress bar:

totalSteps := 10

for i := 0; i <= totalSteps; i++ {
    progress := float64(i) / float64(totalSteps) * 100
    fmt.Fprintf(writer, "Progress: [%s] %.0f%%\n", renderProgressBar(i, totalSteps, 20), progress)
    time.Sleep(time.Millisecond * 100)
}

The renderProgressBar function is a custom helper function that generates a string representation of the progress bar:

func renderProgressBar(current, total, width int) string {
    filled := (current * width) / total
    empty := width - filled

    return strings.Repeat("�?", filled) + strings.Repeat("�?", empty)
}

Finally, stop the uilive.Writer and print a completion message:

writer.Stop()
fmt.Println("Task completed!")

When you run this program, you’ll see a progress bar that updates in real-time as the simulated task completes.

Summary

In this article, we learned how to use the uilive package in Go to create terminal applications with real-time updates. We demonstrated a simple example of a progress bar that updates as a simulated task completes. The uilive package is a powerful tool for creating dynamic, user-friendly terminal applications in Go.

Remember to experiment and explore the other capabilities of the uilive package, such as creating custom animations and handling multiple lines of output. The possibilities are endless!

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-04-29.